﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMetalExtended.BasicTemplate
{
    public static class BuildCode
    {

        private static string ClassWrapperStart(string ns)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("using System;{0}", Environment.NewLine);
            sb.AppendFormat("using System.Collections.Generic;{0}", Environment.NewLine);
            sb.AppendFormat("using System.Linq;{0}", Environment.NewLine);
            sb.AppendFormat("using System.Text;{0}", Environment.NewLine);
            sb.AppendFormat("using Microsoft.SharePoint.Linq;{0}", Environment.NewLine);
            sb.AppendFormat("using Microsoft.SharePoint;{0}", Environment.NewLine);
            sb.AppendFormat("using Microsoft.SharePoint.Publishing.Fields;{0}", Environment.NewLine);
            sb.AppendFormat("{0}namespace {1}{0}", Environment.NewLine, ns);
            sb.AppendFormat("{{{0}", Environment.NewLine);
            return sb.ToString();
        }

        private static string ClassWrapperEnd()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}{0}}}", Environment.NewLine);
            return sb.ToString();
        }

        //public static string Taxonomy(string nodeTitle, string listName, string strNamespace)
        //{
        //      // Andrew Connell solution to taxonmony fields   
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append(ClassWrapperStart(strNamespace));
        //    sb.AppendFormat("{0}{3}public partial class {1}Item : {2}.SPData.Item, ICustomMapping{0}{3}{{{0}{0}", Environment.NewLine, listName, strNamespace, (char)9);

        //    sb.AppendFormat("{0}{1}#region TaxonomyField{0}{0}", Environment.NewLine, (char)9);
        //    sb.AppendFormat("{2}private const string CONTENT_FIELD = \"{1}\";{0}{0}", Environment.NewLine, nodeTitle, (char)9);
        //    sb.AppendFormat("{1}private TaxonomyField _contentTaxonomy;{0}", Environment.NewLine, (char)9);
        //    sb.AppendFormat("{1}public TaxonomyField ContentTaxonomy{0}", Environment.NewLine, (char)9);
        //    sb.AppendFormat("{0}{{", (char)9);
        //    sb.AppendFormat("{0}{1}{1}get{0}{1}{1}{1}{{{0}return _contentTaxonomy;{0}{1}{1}{1}}}", Environment.NewLine, (char)9);
        //    sb.AppendFormat("{0}{1}{1}set{0}{1}{1}{1}{{{0}{1}{1}if (value != _contentTaxonomy){0}", Environment.NewLine, (char)9);
        //    sb.AppendFormat("{1}{1}{{{0}{1}{1}this.OnPropertyChanging(CONTENT_FIELD, _contentTaxonomy);{0}{1}{1}_contentTaxonomy = value;{0}{1}{1}this.OnPropertyChanged(CONTENT_FIELD);{0}{1}{1}}}{0}{1}}}{0}}}{0}", Environment.NewLine, (char)9);
        //    ////    sb.AppendFormat("{0}", Environment.NewLine);
        //    sb.AppendFormat("{0}#endregion{0}{0}", Environment.NewLine);

        //    sb.AppendFormat("{0}[CustomMapping(Columns = new string[] {{ CONTENT_FIELD }})]{0}", Environment.NewLine);
        //    sb.AppendFormat("{0}public void MapFrom(object listItem){0}{{{0}", Environment.NewLine);
        //    sb.AppendFormat("SPListItem item = (SPListItem)listItem;{0}", Environment.NewLine);
        //    sb.AppendFormat("this.ContentTaxonomy = item[CONTENT_FIELD ] as TaxonomyField;{0}}}{0}", Environment.NewLine);

        //    sb.AppendFormat("{0}public void MapTo(object listItem)", Environment.NewLine);
        //    sb.AppendFormat("{0}{{{0}{0}}}{0}", Environment.NewLine);
        //    sb.AppendFormat("{0}public void Resolve(RefreshMode mode, object originalListItem, object databaselistItem)", Environment.NewLine);
        //    sb.AppendFormat("{0}{{{0}{0}}}{0}", Environment.NewLine);

        //    sb.AppendFormat("{0}}}{0}{0}", Environment.NewLine);
        //    sb.Append(ClassWrapperEnd());
        //    return sb.ToString();
        //}

        public static string PublishingImage(string nodeTitle, string listName, string strNamespace)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(ClassWrapperStart(strNamespace));
            sb.AppendFormat("{0}{3}public partial class {1}Item : {2}.SPData.Item, ICustomMapping{0}{3}{{{0}{0}", Environment.NewLine, listName, strNamespace, (char)9);
            sb.AppendFormat("{0}{1}#region Publishing Image{0}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{2}private const string CONTENT_FIELD = \"{1}\";{0}{0}", Environment.NewLine, nodeTitle, (char)9);
            sb.AppendFormat("{1}private ImageFieldValue _contentImage;{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{1}public ImageFieldValue ContentImage{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{{", (char)9);
            sb.AppendFormat("{0}{1}{1}get{0}{1}{1}{{{0}{1}{1}{1}return _contentImage;{0}{1}{1}}}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}{1}set{0}{1}{1}{{{0}{1}{1}{1}if (value != _contentImage){0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{1}{1}{1}{{{0}{1}{1}{1}{1}this.OnPropertyChanging(CONTENT_FIELD, _contentImage);{0}{1}{1}{1}{1}_contentImage = value;{0}{1}{1}{1}{1}this.OnPropertyChanged(CONTENT_FIELD);{0}{1}{1}{1}}}{0}{1}{1}}}{0}{1}}}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}#endregion{0}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}[CustomMapping(Columns = new string[] {{ CONTENT_FIELD }})]{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{1}public void MapFrom(object listItem){0}{1}{{{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{1}{1}SPListItem item = (SPListItem)listItem;{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{1}{1}this.ContentImage = item[CONTENT_FIELD ] as ImageFieldValue;{0}{1}}}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}public void MapTo(object listItem)", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}{{{0}{1}{1}SPListItem item = (SPListItem)listItem;{0}{1}{1}item[CONTENT_FIELD] = this.ContentImage;{0}{1}}}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}public void Resolve(RefreshMode mode, object originalListItem, object databaselistItem)", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}{{{0}{0}{1}}}{0}", Environment.NewLine, (char)9);
            sb.AppendFormat("{0}{1}}}{0}", Environment.NewLine, (char)9);
            sb.Append(ClassWrapperEnd());
            return sb.ToString();
        }

    }
}
